package com.itms.tool.data;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Object {

	public static final String IS_SENT = "isSent";
	public static final String DATE_CREATED = "dateCreated";

	@DatabaseField(generatedId = true)
	private int id;
	@DatabaseField
	private String name;
	@DatabaseField
	private float rating = 3.5f;
	@DatabaseField
	private double longitude;
	@DatabaseField
	private double latitude;
	@DatabaseField(columnName = IS_SENT)
	private boolean isSent;
	@DatabaseField(columnName = DATE_CREATED)
	private Date dateCreated;
	@DatabaseField
	private Date dateSent;
	@DatabaseField(foreign = true, foreignAutoRefresh = true)
	private Category category;
	@DatabaseField
	private String city;
	@ForeignCollectionField(eager = true)
	private ForeignCollection<Image> images;
	@ForeignCollectionField
	private ForeignCollection<ObjectTopic> objectTopics;

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getRating() {
		return rating;
	}

	public void setRating(float rating) {
		this.rating = rating;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public boolean isSent() {
		return isSent;
	}

	public void setSent(boolean isSent) {
		this.isSent = isSent;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public Date getDateSent() {
		return dateSent;
	}

	public void setDateSent(Date dateSent) {
		this.dateSent = dateSent;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		if (category != null && category.getParentCategory() == null) {
			throw new IllegalArgumentException(
					"object only accepts sub-categories");
		}
		this.category = category;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public ForeignCollection<Image> getImages() {
		return images;
	}

	public List<Image> getImageList() {
		List<Image> imgList = new ArrayList<Image>();
		if (images != null) {
			Iterator<Image> iterator = images.iterator();
			while (iterator.hasNext()) {
				imgList.add(iterator.next());
			}
		}
		return imgList;
	}

	public ForeignCollection<ObjectTopic> getObjectTopics() {
		return objectTopics;
	}

	public List<Topic> getTopics() {
		List<Topic> topics = new ArrayList<Topic>();
		if (objectTopics != null) {
			CloseableIterator<ObjectTopic> iterator = objectTopics
					.closeableIterator();
			try {
				while (iterator.hasNext()) {
					topics.add(iterator.next().getTopic());
				}
			} finally {
				try {
					iterator.close();
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
			}
		}
		return topics;
	}

	@Override
	public int hashCode() {
		return 31 * id;
	}
	
	@Override
	public boolean equals(java.lang.Object o) {
		if (!(o instanceof Object)) {
			return false;
		}
		return id == ((Object)o).id;
	}
	
	@Override
	public String toString() {
		return "Object[name=" + name + ",rating=" + rating + ",longitude="
				+ longitude + ",latitude=" + latitude + ",city=" + city
				+ ",dateCreated=" + dateCreated + "]";
	}
}
